$(function () {
    //表格初始化
    $("#cus_grid").datagrid({
        /*自适应*/
        fit: true,
        /*向此地址发送请求获得数据填充表格*/
        url: '/customer/query.do?state=true',
        columns: [/*此处为第一层列标题*/[
            {field: "name", title: "客户姓名", width: 100},
            {
                field: "gender", title: "性别", width: 100, formatter: function (value) {
                return value ? "<p>男</p>" : "<p>女</p>"
            }
            },
            {field: "age", title: "年龄", width: 100},
            {field: "address", title: "客户地址", width: 100},
            {field: "idcard", title: "客户身份证号", width: 100},
            {
                field: "inchargeuser", title: "责任人", width: 100, formatter: function (value) {
                return value ? value.username : ""
            }
            },
            {
                field: "inputuser", title: "录入人", width: 100, formatter: function (value) {
                return value ? value.username : ""
            }
            },
            {field: "date", title: "变更日期", width: 100},
            {field: "job", title: "客户职业", width: 100},
            {field: "customersource", title: "客户来源", width: 100},
            {field: "salarylevel", title: "客户薪资", width: 100},
            {
                field: "abc", title: "联系该客户", width: 100, formatter: function () {
                return "<a href='javascript:opentel()'><p><font color='#ffc0cb'>查看联系方式</font></p></a>"
            }
            },
        ]/*此处可以插入第二层标题*/],
        //为表格顶端的按键
        toolbar: "#tb",
        //真正的自动展开收缩列的大小,以适应网格的宽度,防水平滚动
        fitColumns: true,
        //可在行底显示分页工具栏
        pagination: true,
        //行码
        rownumbers: true,
        //只允许单选某行
        singleSelect: true,
        //允许排序的列默认发送的参数
        //点击一行时出发的事件,其具有两个参数index,row;index为被选中的行的索引从零开始,row为被选中的行
        onClickRow: function (index, row) {
            if (row.inchargeuser) {
                $("#setCharge_btn").linkbutton({
                    text: '<font color="#a52a2a">移交客户</font>'
                })
            } else {
                $("#setCharge_btn").linkbutton({
                    text: '<font color="#008b8b">设置责任人</font>'
                })
            }
        }

    })
    //客户编辑
    $("#emp_dialog").dialog({
        width: 350,
        height: 450,
        resizable: true,
        buttons: '#form_btn',
        closed: true,
        onClose: function () {
            $("#emp_form").form('clear');
        }
    })

    //客户联系方式弹窗初始化
    $("#resetpassword_dialog").dialog({
        width: 720,
        height: 200,
        resizable: true,
        closed: true,
        title: "客户联系方式",
        onClose: function () {
            $("#emp_form").form('clear');
        }
    })
    //放在联系方式弹窗中
    $("#tel_grid").datagrid({
        /*自适应*/
        fit: true,
        /*向此地址发送请求获得数据填充表格*/
        url: '/customer/query.do?state=true',
        columns: [[
            {field: "tel", title: "电话", width: 100},
            {field: "email", title: "邮箱", width: 100},
            {field: "address", title: "客户地址", width: 100},
            {field: "qq", title: "QQ号", width: 100},
            {field: "wechat", title: "微信号", width: 100},
            {
                field: "abc", title: "联系该客户", width: 100, formatter: function () {
                return "<a href='javascript:dotel()'><font color='#ffc0cb'>联系该客户</font></a>"
            }
            },
        ]],
        fitColumns: true,
        singleSelect: true,
    })
    $("#sn_grid").datagrid({
        /*自适应*/
        fit: true,
        /*向此地址发送请求获得数据填充表格*/
        url: '/customer/query.do?state=true',
        columns: [[
            {field: "sn", title: "客户编号", width: 230},
        ]],
        fitColumns: true,
        singleSelect: true,
    })
    $("#tel_dialog").dialog({
        width: 300,
        height: 300,
        resizable: true,
        buttons: '#passform_btn',
        closed: true,
        title: "联系客户",
    })
    $("#look_sn").dialog({
        width: 120,
        height: 100,
        resizable: true,
        closed: true,
        title: "查看客户编号",
    })
    $("#inchargeuser_dialog").dialog({
        width: 320,
        height: 420,
        resizable: true,
        buttons: '#set_btn',
        closed: true,
        title: "客户责任人设置",
        onClose: function () {
            $("#inchargeuser_form").form('clear');
        }
    })
    $("#fail_dialog").dialog({
        width: 320,
        height: 320,
        resizable: true,
        buttons: '#fail_btn',
        closed: true,
        title: "客户开发失败原因",
        onClose: function () {
            $("#faiil_form").form('clear');
        }
    })
})
//看用户编码
function looksn() {
    var row = $("#cus_grid").datagrid("getSelected");
    if (!row) {
        $.messager.alert("温馨提示", "请选中一条数据", "warning");
        return;
    }
    $("#sn_grid").datagrid('load', {id: row.id});
    $("#look_sn").dialog('open');
}
//看用户联系信息
function opentel() {
    var row = $("#cus_grid").datagrid("getSelected");
    if (!row) {
        $.messager.alert("温馨提示", "请选中一条数据", "warning");
        return;
    }
    $("#tel_grid").datagrid('load', {id: row.id});
    $("#resetpassword_dialog").dialog('open');
}
//联系客户
function dotel() {
    $("#tel_dialog").dialog('open');
}
//添加客户
function add() {
    $("#emp_dialog").dialog('setTitle', '新增客户');
    $("#emp_dialog").dialog('open');
}
//编辑客户
function edit() {
    var row = $("#cus_grid").datagrid("getSelected");
    if (!row) {
        $.messager.alert("温馨提示", "请选中一条数据", "warning");
        return;
    }
    //回显表单,因为在row中现实的是dept.name所以只能用该方法进行部门id的辉县用于下拉列表
    if (row.inchargeuser) {
        row["inchargeuser.username"] = row.inchargeuser.username;
    }
    if (row.inputuser) {
        row["inputuser.username"] = row.inputuser.username;
    }
    $("#emp_form").form('load', row);
    //打开弹窗
    $("#emp_dialog").dialog('setTitle', '编辑客户信息');
    $("#emp_dialog").dialog('open');
}
//刷新页面
function reload() {
    $("#cus_grid").datagrid('reload');
}
//查询
function query() {
    var keyword = $("[name=keyword]").val();
    var deptId = $("[name=deptId]").val();
    $("#emp_grid").datagrid('load', {
        keyword: keyword,
        deptId: deptId
    })
}
//表单中的按键
function save() {
    $("#emp_form").form('submit', {
        url: '/customer/saveOrUpdate.do',
        success: function (data) {
            data = $.parseJSON(data);
            if (data.success) {
                $.messager.alert('温馨提示', '保存成功！', 'info', function () {
                    cancel();
                    $("#cus_grid").datagrid('reload');
                });
            } else {
                $.messager.alert('温馨提示', data.msg, 'warning');
            }
        }
    })
}
//取消提交表单
function cancel() {
    $("#emp_dialog").dialog('close');
}
function dcancel() {
    $("#inchargeuser_dialog").dialog('close');
}
//设置责任人
function setCharge() {
    //获取当前行
    var row = $("#cus_grid").datagrid("getSelected");
    if (!row) {
        $.messager.alert("温馨提示", "请选中一条数据", "warning")
        return;
    }
    //设置键的功能
    if (row.inchargeuser) {
        $("#inchargeuser_dialog").dialog("setTitle", "移交客户");
        $("#abc").textbox("setValue", row.inchargeuser.username)
        $("#current_tr").show();
        $("#reason_tr").show();
        $("#move_td").text("移交给:");
    } else {
        $("#inchargeuser_dialog").dialog("setTitle", "设置责任人");
        $("#current_tr").hide();
        $("#reason_tr").hide();
        $("#move_td").text("设置责任人:");
    }
    //打开弹窗
    if (row.inputuser) {
        row["inputuser.id"] = row.inputuser.id;
    }
    $("#inchargeuser_form").form('load', row);
    $("#inchargeuser_dialog").dialog("open");
}
//责任人表单提交
function setsave() {
    var row = $("#cus_grid").datagrid("getSelected");
    $("#inchargeuser_form").form('submit', {
        url: '/customer/setInchargeuser.do',
        success: function (data) {
            data = $.parseJSON(data);
            if (data.success) {
                $.messager.alert('温馨提示', '保存成功！', 'info', function () {
                    dcancel();
                    $("#cus_grid").datagrid('reload');
                });
            } else {
                $.messager.alert('温馨提示', data.msg, 'warning');
            }
        }
    })
    console.log(row.inchargeuser);
    if (row.inchargeuser) {
        $("#inchargeuser_form").form('submit', {
            url: '/custurn/save.do',
        })
    }
}
//责任人表单关闭
function setcancel() {
    $("#inchargeuser_dialog").dialog("close");
}
//重置查询条件
function reset(){
    $.messager.confirm("提示信息","请问您是否确认重置查询条件",function (a) {
        if (a){
            $(".qeury_date_text").datebox("setValue","");
            $(".qeury_text").textbox("clear");
            query();
        }else{
            return;
        }
    })

}
//查询
function query() {
    var startDate = $("[name=startDate]").val();
    var endDate = $("[name=endDate]").val();
    var custom = $("[name=custom]").val();
    var salary = $("[name=salary]").val();
    var job = $("[name=job]").val();
    var charge = $("[name=charge]").val();
    $("#cus_grid").datagrid('load', {
        startDate: startDate,
        endDate:endDate,
        custom:custom,
        salary:salary,
        job:job,
        charge:charge
    })
}
function changeState() {
    var row = $("#cus_grid").datagrid('getSelected');
    if (!row) {
        $.messager.alert('温馨提示', '请选中一条数据', 'warning');
        return;
    }
    $.messager.confirm('确认对话框', '您确认要执行操作吗?', function (ro) {
        if (ro) {
            $.post('/customer/moveResource.do', {id: row.id}, function (data) {
                if (data.success) {
                    $.messager.alert('温馨提示', '您的操作已经成功', 'info', function () {
                        $("#cus_grid").datagrid('reload');
                    })
                } else {
                    $.messager.alert('温馨提示', data.msg, 'warning');
                }
            })
        }
    })
}

function failsave() {
    $("#faiil_form").form('submit', {
        url: '/faillog/saverun.do',
    })
    $("#faiil_form").form('submit', {
        url: '/customer/delete.do',
        success: function (data) {
            data = $.parseJSON(data);
            if (data.success) {
                $.messager.alert('温馨提示', '保存成功！', 'info', function () {
                    failcancel();
                    $("#cus_grid").datagrid('reload');
                });
            } else {
                $.messager.alert('温馨提示', data.msg, 'warning');
            }
        }
    })
}
function failcancel() {
    $("#fail_dialog").dialog("close")
}
function faillog() {
    var row = $("#cus_grid").datagrid('getSelected');
    if (!row) {
        $.messager.alert('温馨提示', '请选中一条数据', 'warning');
        return;
    }
    $("#faiil_form").form("load",row)
    $.messager.confirm('确认对话框', '您确定' + row.name + '开发失败吗?', function (ro) {
        if (ro) {
            if (row.inchargeuser) {
                $("#charge").textbox("setValue", row.inchargeuser.username)
            }
            $("#fail_dialog").dialog("open");

        }
    })
}